package com.jdbc.dataAccess.Oracle.Utilities;

import com.jdbc.dataAccess.ConnectionSettings;

/******************************************************************************
 *
 * @autor   robert.baron
 * @since       02-abr-2013
 *
 *******************************************************************************/
public class ConnectionHelper {

      private static final String oracleDriver = "oracle.jdbc.driver.OracleDriver";
      
      public static String getConnectionString(ConnectionSettings connSettings){
            
            if (connSettings == null) throw new IllegalArgumentException("ConnectionSettings can't be null");
            
            String connectionString = "URL="+
                    getUrl(connSettings) + ";"+
                    "USER=" + connSettings.getUser() + ";" +
                    "PASS=" + connSettings.getPassword() + ";";
            
            return connectionString;
      }
      
      public static String getUrl(ConnectionSettings connSettings){
            
            return "jdbc:" + connSettings.getDbms() + ":@"
                    + connSettings.getServerName()
                    + ":" + connSettings.getPort() + ":"
                    + connSettings.getDatabase();
      }
      
      public static ConnectionSettings getConnectionSettings(String connectionString) {

            ConnectionSettings connSettings = new ConnectionSettings();

            String[] connection = connectionString.split(";");

            String strUrl = null;
            String strPass = null;
            String strUser = null;
            
            for (int indice = 0; indice < 3; indice++) {
                  if (connection[indice].toUpperCase().contains("URL")) {
                        strUrl = connection[indice].trim().replace(" ", "");
                        strUrl = strUrl.substring(4, strUrl.length());
                  }
                  if (connection[indice].toUpperCase().contains("USER")) {
                        strUser = connection[indice].trim().replace(" ", "");
                        strUser = strUser.substring(5, strUser.length());
                  }
                  if (connection[indice].toUpperCase().contains("PASS")) {
                        strPass = connection[indice].trim().replace(" ", "");
                        strPass = strPass.substring(5, strPass.length());
                  }
            }

            connSettings.setServerName(getServer(strUrl));
            connSettings.setPort(getPort(strUrl));
            connSettings.setDatabase(getDatabaseName(strUrl));
            connSettings.setDbms(getDbms(strUrl));
            connSettings.setUrl(strUrl);
            connSettings.setDriver(oracleDriver);
                        
            connSettings.setUser(strUser);
            connSettings.setPassword(strPass);

            connSettings.setConnectionString(connectionString);

            return connSettings;
      }

      public static String getServer(String URL) {
          return  URL.split(":")[3].replace("@", "");
      }

      public static String getPort(String URL) {
            return  URL.split(":")[4];
      }

      public static String getDatabaseName(String URL) {
            return  URL.split(":")[5];
      }

      public static String getDbms(String URL) {       
                  return URL.substring(5, URL.indexOf("@") - 1);            
      }
}
